package com.teamin.web.appListener;

import com.mysql.cj.core.util.StringUtils;
import com.teamin.web.socket.SubscribeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;

public class SessionUnsubscribeListener implements ApplicationListener<SessionUnsubscribeEvent> {
	private static final Logger LOGGER = LoggerFactory.getLogger(SessionUnsubscribeListener.class);

	@Override
	public void onApplicationEvent(SessionUnsubscribeEvent event) {

		StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
		if(!StringUtils.isNullOrEmpty(SubscribeManager.getSubId(event.getUser().getName(),accessor.getDestination()))) {
			SubscribeManager.deleteSub(event.getUser().getName(),accessor.getDestination()); ;
		}
		LOGGER.info("SessionUnsubscribeEvent timestamp: " + event.getTimestamp());
		LOGGER.info("SessionUnsubscribeEvent user: " + event.getUser());
		LOGGER.info("SessionUnsubscribeEvent: " + event.toString());
	}

}
